package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import io.lettuce.core.output.ListOfGenericMapsOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 徐扬文
 * @version 1.0
 */
@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void save(AddressBook addressBook) {
        log.info("新增地址簿:{}", addressBook);
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.insert(addressBook);
    }

    /**
     * 查询当前登录用户的所有地址
     * @return
     */
    @Override
    public List<AddressBook> list(AddressBook addressBook) {
        log.info("查询当前登录用户所有地址信息");
        List<AddressBook> list = addressBookMapper.list(addressBook);
        return list;
    }

    /**
     * 设置默认地址
     */
    @Override
    public void setDefault(AddressBook addressBook) {
        log.info("设置默认地址:{}", addressBook);
        // 1.将当前登录用户的所有地址的is_default字段置0
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateIsDefaultByUserId(addressBook);
        // 2.将当前地址的is_default字段置1
        addressBook.setIsDefault(1);
        addressBookMapper.setDefault(addressBook);
    }

    /**
     * 根据id查询地址
     */
    @Override
    public AddressBook getById(Long id) {
        log.info("根据id查询地址:{}", id);
        AddressBook addressBook = addressBookMapper.getById(id);
        return addressBook;
    }

    /**
     * 修改地址
     */
    @Override
    public void update(AddressBook addressBook) {
        log.info("修改地址:{}", addressBook);
        addressBookMapper.update(addressBook);
    }

    /**
     * 删除地址
     */
    @Override
    public void delete(Long id) {
        log.info("删除地址:{}", id);
        addressBookMapper.delete(id);
    }
}
